/**
 * @file cann_info_utils.h
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2019-2024. All rights reserved.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef ASCEND_CANN_INFO_UTILS_H
#define ASCEND_CANN_INFO_UTILS_H

#include <limits>
#include <mutex>
#include <vector>
#include <string>
#include <map>

#include "runtime/base.h"
#include "acl/acl_base.h"

namespace acl {
    constexpr const size_t MAX_CANN_ATTR_SIZE = 128U;
    // if version info is not set in swFeatureList.json, use UNKNOWN_VERSION as default value
    constexpr const int32_t UNKNOWN_VERSION = -1;
    constexpr const char_t *const SW_CONFIG_RUNTIME = "runtimeVersion";
    constexpr const char_t *const SW_CONFIG_SOC_VERSION = "socVersionList";

    struct CannInfo {
        std::string configKeyName;
        int32_t runtimeVersion;
        std::vector<std::string> socVersions;
        int32_t isAvailable = 0;
        explicit CannInfo(const std::string &name, const int32_t runtime = UNKNOWN_VERSION) noexcept
            : configKeyName(name), runtimeVersion(runtime)
        {
        }
    };

    class CannInfoUtils {
    public:
        static aclError GetAttributeList(const aclCannAttr **cannAttr, size_t *num);
        static aclError GetAttribute(aclCannAttr cannAttr, int32_t *value);
        static aclError ParseVersionValue(const std::string &str, int32_t *value);

    private:
        static aclError Initialize();
        static aclError GetConfigInstallPath();
        static aclError ParseVersionInfo(const std::string &path, int32_t *version);
        static bool MatchVersionInfo(const CannInfo &configCannInfo);
        static bool MatchSocVersion(const std::vector<std::string> &swConfigSocVersions);
        static void CheckAndUpdateAttrAvailability();

        static std::mutex mutex_;
        static bool initFlag_;
        static CannInfo currCannInfo_;
        static std::string swConfigPath_;
        static std::string defaultInstallPath_;
        static aclCannAttr attrArray_[MAX_CANN_ATTR_SIZE];
        static size_t attrNum_;
        static std::map<aclCannAttr, CannInfo> attrToCannInfo_;
    };
} // namespace acl

#endif // ASCEND_CANN_INFO_UTILS_H
